package com.waters321.rock.orm.config.mybatisplus;

import java.util.Date;
import org.apache.ibatis.reflection.MetaObject;
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.waters321.rock.orm.entity.BaseEntity;
import com.waters321.rock.orm.enums.DeleteFlagEnum;

public class AutoFillMetaObjectHandler extends MetaObjectHandler {

  @Override
  public void insertFill(MetaObject metaObject) {
    // 系统级
    Object deleteFlag = getFieldValByName(BaseEntity.DELETE_FLAG, metaObject);
    if (deleteFlag == null) {
      setFieldValByName(BaseEntity.DELETE_FLAG, DeleteFlagEnum.NORMAL, metaObject);
    }

    Object createAt = getFieldValByName(BaseEntity.CREATE_AT, metaObject);
    if (createAt == null) {
      setFieldValByName(BaseEntity.CREATE_AT, new Date(), metaObject);
    }

    Object createBy = getFieldValByName(BaseEntity.CREATE_BY, metaObject);
    if (createBy == null) {
      setFieldValByName(BaseEntity.CREATE_BY, 0L, metaObject);
    }

    Object createByName = getFieldValByName(BaseEntity.CREATE_BY_NAME, metaObject);
    if (createByName == null) {
      setFieldValByName(BaseEntity.CREATE_BY_NAME, "系统", metaObject);
    }

    Object version = getFieldValByName(BaseEntity.VERSION, metaObject);
    if (version == null) {
      setFieldValByName(BaseEntity.VERSION, 1, metaObject);
    }

  }

  @Override
  public void updateFill(MetaObject metaObject) {
    // 系统级
    setFieldValByName(BaseEntity.UPDATE_AT, new Date(), metaObject);
    setFieldValByName(BaseEntity.UPDATE_BY, 0L, metaObject);
    setFieldValByName(BaseEntity.UPDATE_BY_NAME, "系统", metaObject);

  }

}
